#include <string.h>
#include <stdlib.h>

#include "data_process.h"
#include "data.h"

// 判断节点中某变量是否存在的函数
bool adminAccountExists(struct admin *head, char *account) {
    // 用于判断某个管理员账号是否已经存在
    struct admin *currentNode = head;
    while (currentNode != NULL) {
        if (strcmp(currentNode->account, account) == 0) {
            return true;
        }
        currentNode = currentNode->next;
    }
    return false;
}

bool userNameExists(struct user *head, char *username) {
    struct user *currentNode = head;
    while (currentNode != NULL) {
        if (strcmp(currentNode->username, username) == 0) {
            return true;
        }
        currentNode = currentNode->next;
    }
    return false;
}

bool flightNumberExists(struct flight *head, char *flightNumber) {
    struct flight *currentNode = head;
    while (currentNode != NULL) {
        if (strcmp(currentNode->number, flightNumber) == 0) {
            return true;
        }
        currentNode = currentNode->next;
    }
    return false;
}

bool orderFlightNumberExists(struct order *head, char *flightNumber) {
    struct order *currentNode = head;
    while (currentNode != NULL) {
        if (strcmp(currentNode->flightNumber, flightNumber) == 0) {
            return true;
        }
        currentNode = currentNode->next;
    }
    return false;
}

bool orderUsernameExists(struct order *head, char *username) {
    struct order *currentNode = head;
    while (currentNode != NULL) {
        if (strcmp(currentNode->userName, username) == 0) {
            return true;
        }
        currentNode = currentNode->next;
    }
    return false;
}

bool orderSeatNumberExists(struct order *head, unsigned int seat) {
    struct order *currentNode = head;
    while (currentNode != NULL) {
        if (currentNode->seat == seat) {
            return true;
        }
        currentNode = currentNode->next;
    }
    return false;
}

// 插入节点的函数，统一在链表头部插入节点
void addToAdminHead(struct admin **head, struct admin *node) {
    if (*head == NULL) {
        *head = node;
        node->next = NULL;
    } else {
        node->next = (*head)->next;
        (*head)->next = node;
    }
}

void addToFlightHead(struct flight **head, struct flight *node) {
    if (*head == NULL) {
        *head = node;
        node->next = NULL;
    } else {
        node->next = *head;
        *head = node;
    }
    node = NULL;
}

void addToUserHead(struct user **head, struct user *node) {
    if (*head == NULL) {
        *head = node;
        node->next = NULL;
    } else {
        node->next = *head;
        *head = node;
    }
}

void addToOrderHead(struct order **head, struct order *node) {
    if (*head == NULL) {
        *head = node;
        node->next = NULL;
    } else {
        node->next = *head;
        *head = node;
    }
}

void deleteFlightByNumber(struct flight **head, char *flightNumber) {
    if (*head == NULL) {
        // 链表为空，直接返回
        return;
    } else {
        // node是将要删除的节点
        struct flight *node = (*head)->next, *before = (*head);
        if (node == NULL) {
            *head = NULL;
        }
        while (node != NULL) {
            if (strcmp(node->number, flightNumber) == 0) {
                before->next = node->next;
                node = before->next;
            } else {
                node = node->next;
                before = before->next;
            }
        }
    }
}

void deleteOrderByNumber(struct order **head, char *flightNumber) {
    if (*head == NULL) {
        // 链表为空，直接返回
        return;
    } else {
        // node是将要删除的节点
        struct order *node = (*head)->next, *before = (*head);
        if (node == NULL) {
            *head = NULL;
        }
        while (node != NULL) {
            if (strcmp(node->flightNumber, flightNumber) == 0) {
                before->next = node->next;
                node = before->next;
            } else {
                node = node->next;
                before = before->next;
            }
        }
    }
}

bool deleteOrderByNumber1(struct order** orderHead, struct flight** flightHead, char* number) {
    if (*orderHead == NULL) {
        // 链表为空，直接返回
        return false;
    }
    else {
        // node是将要删除的节点
        struct order* node = (*orderHead)->next, * before = (*orderHead);
        struct flight* flightNode;
        if (node == NULL) {
            *orderHead = NULL;
        }
        while (node != NULL) {
            if (strcmp(node->flightNumber, number) == 0) {
                flightNode = *flightHead;
                while (flightNode != NULL) {
                    if (strcmp(flightNode->number, number) == 0) {
                        flightNode->bookTypes[node->seat - 1] = UNBOOKED;
                    }
                    flightNode = flightNode->next;
                }
                before->next = node->next;
                node = before->next;
            }
            else if (strcmp(before->flightNumber, number) == 0) {
                flightNode = *flightHead;
                while (flightNode != NULL) {
                    if (strcmp(flightNode->number, number) == 0) {
                        flightNode->bookTypes[before->seat - 1] = UNBOOKED;
                    }
                    flightNode = flightNode->next;
                }
                (*orderHead) = before->next;
                before = (*orderHead);
                node = (*orderHead)->next;
            }
            else {
                node = node->next;
                before = before->next;
            }
        
        }
        return true;
    }
}

void deleteUserByUsername(struct user **head, char *username) {
    if (*head == NULL) {
        // 链表为空，直接返回
        return;
    } else {
        // node是将要删除的节点
        struct user *node = (*head)->next, *before = (*head);
        if (node == NULL) {
            *head = NULL;
        }
        while (node != NULL) {
            if (strcmp(node->username, username) == 0) {
                before->next = node->next;
                node = before->next;
            } else {
                node = node->next;
                before = before->next;
            }
        }
    }
}
